﻿using System.Net.Http;
using System.Threading.Tasks;
using System;
using ShanjinPDAUI.ModelEntity;
using System.Text;
using System.Text.Json;
using ShanjinPDAUI.Commom;
namespace ShanjinPDAUI.Data
{
    public class AG2WareHouseServer
    {
        public async Task<string> ClearAG2WareHouse(string remark,int materialType)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/AG2WareHouse/ClearWareHouse?Remark={remark}&MaterialType={materialType}");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {

                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }
        }
        public async Task<AG2Warehouse_Entity> GetAG2Warehouse_Entity(string remark)
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/AG2WareHouse/GetAG2Warehouse?Remark={remark}");
                    request.Headers.Add("Accept", "text / plain");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {

                            var result = await response.Content.ReadAsStreamAsync();

                            return JsonSerializer.Deserialize<AG2Warehouse_Entity>(result);
                        }
                        else
                        {
                            return null;
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return null;
                    }
                }
            }
        }
        public async Task<string> CreatOutBoundTask(string remark)
        {
            var par = new OutboundPar();
            par.TASK_NO = String.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now) + "PDA";
            par.FROM_POSITION = "K3TAGR102_2";
            par.INPUT_POSITION = remark;
            var jsonPar = JsonSerializer.Serialize<OutboundPar>(par);
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, $"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/TaskCreat/Outbound");
                    request.Headers.Add("Accept", "text / plain");
                    request.Content = new StringContent(jsonPar, Encoding.UTF8, "application/json");
                    try
                    {
                        var response = await client.SendAsync(request);
                        if (response.IsSuccessStatusCode)
                        {
                            //using var responseStream = await response.Content.ReadAsStreamAsync();
                            //var result = await JsonSerializer.DeserializeAsync
                            //    <string>(responseStream);
                            var result = await response.Content.ReadAsStringAsync();
                            return result;
                        }
                        else
                        {
                            return "调用接口失败";
                            //  getBranchesError = true;
                        }
                    }
                    catch (Exception e)
                    {

                        return "调用接口异常";
                    }
                }
            }
        }
    }
}
